﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace QuanLyPhongKham
{
    public partial class ucVietTat : BaseUserControl
    {
        private DAL.AccessData m;
        private DataTable dt = new DataTable();
        private long l_id = 0;
        public ucVietTat(DAL.AccessData acc)
        {
            this.m = acc;
            InitializeComponent();
        }

        private void frmViettat_Load(object sender, EventArgs e)
        {
            load_grid();
            reft_text();
            ena_button(true);
            AddGridTableStyle();
        }
        private void load_grid()
        {
            dt = m.get_data("select * from " + m.user + ".dmviettat").Tables[0];
            dataGrid1.DataSource = dt;
        }
        private void AddGridTableStyle()
        {
            DataGridColoredTextBoxColumn TextCol;
            delegateGetColorRowCol de = new delegateGetColorRowCol(MyGetColorRowCol);
            DataGridTableStyle ts = new DataGridTableStyle();
            ts.MappingName = dt.TableName;
            ts.AlternatingBackColor = Color.Linen;
            ts.BackColor = Color.GhostWhite;
            ts.ForeColor = Color.MidnightBlue;
            ts.GridLineColor = SystemColors.Control;
            ts.HeaderBackColor = SystemColors.Control;
            ts.HeaderForeColor = Color.Navy;
            ts.SelectionBackColor = Color.Teal;
            ts.SelectionForeColor = Color.White;
            ts.ReadOnly = true;
            ts.RowHeaderWidth = 5;

            TextCol = new DataGridColoredTextBoxColumn(de, 0);
            TextCol.MappingName = "id";
            TextCol.HeaderText = "";
            TextCol.Width = 0;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);


            TextCol = new DataGridColoredTextBoxColumn(de, 1);
            TextCol.MappingName = "tenvt";
            TextCol.HeaderText = "Viết tắt";
            TextCol.Width = 120;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);

            TextCol = new DataGridColoredTextBoxColumn(de, 2);
            TextCol.MappingName = "diengiai";
            TextCol.HeaderText = "Diễn giải";
            TextCol.Width = 460;
            ts.GridColumnStyles.Add(TextCol);
            dataGrid1.TableStyles.Add(ts);
        }
        public Color MyGetColorRowCol(int row, int col)
        {
            Color c = Color.Navy;
            try
            {
                if (row == dataGrid1.CurrentRowIndex)
                {
                    dataGrid1.Select(row);
                    c = Color.White;
                }
            }
            catch
            {
            }
            return c;
        }
        public delegate Color delegateGetColorRowCol(int row, int col);
        public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
        {
            private delegateGetColorRowCol _getColorRowCol;
            private int _column;
            public DataGridColoredTextBoxColumn(delegateGetColorRowCol getcolorRowCol, int column)
            {
                _getColorRowCol = getcolorRowCol;
                _column = column;
            }
            protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
            {
                try
                {
                    foreBrush = new SolidBrush(_getColorRowCol(rowNum, this._column));
                }
                catch { }
                finally
                {
                    base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
                }
            }
        }
        private void ena_button(bool ena)
        {
            butMoi.Enabled = ena;
            butLuu.Enabled = !ena;
            butSua.Enabled = ena;
            butHuy.Enabled = ena;
            butBoqua.Enabled = !ena;
            butKetthuc.Enabled = ena;
        }
        private void ena_object(bool ena)
        {
            mavt.Enabled = ena;
            diengiai.Enabled = ena;
        }

        private void butMoi_Click(object sender, EventArgs e)
        {
            l_id = 0;
            mavt.Text = ""; diengiai.Text = "";
            ena_button(false);
            ena_object(true);
            mavt.Focus();
        }

        private void butSua_Click(object sender, EventArgs e)
        {
            ena_button(false);
            ena_object(true);
            mavt.Focus();
        }

        private void butBoqua_Click(object sender, EventArgs e)
        {
            ena_object(false); ena_button(true);
            butMoi.Focus();
            reft_text();
        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            ena_object(false);
            reft_text();
        }
        private void reft_text()
        {
            try
            {
                l_id = long.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber, 0].ToString());
                mavt.Text = dataGrid1[dataGrid1.CurrentCell.RowNumber, 1].ToString();
                diengiai.Text = dataGrid1[dataGrid1.CurrentCell.RowNumber, 2].ToString();
            }
            catch { }
        }

        private void butLuu_Click(object sender, EventArgs e)
        {
            l_id = (l_id == 0) ? m.get_capid_viettat : l_id;
            if (mavt.Text.Trim() == "" || diengiai.Text.Trim() == "")
            {
                MessageBox.Show("Nhập thông tin!",DAL.AccessData.Msg);
                diengiai.Focus(); return;
            }
            m.upd_viettat(l_id, mavt.Text.ToLower(), diengiai.Text);
            load_grid();
            reft_text();
            ena_button(true);
            ena_object(false);
            butMoi.Focus();
        }

        private void mavt_Validated(object sender, EventArgs e)
        {
            if (mavt.Text.Trim() != "")
            {
                DataRow r = m.getrowbyid(dt, "tenvt='" + mavt.Text.Trim() + "'");
                if (r != null)
                {
                    l_id = long.Parse(r["id"].ToString());
                    diengiai.Text = r["diengiai"].ToString();
                }
            }
        }

        private void mavt_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
            }
        }

        private void butKetthuc_Click(object sender, EventArgs e)
        {
            //fix:
            //this.Close();
        }

        private void butHuy_Click(object sender, EventArgs e)
        {
            if (l_id == 0) return;
            if (MessageBox.Show("Bạn có đồng ý huỷ thông tin này không?", DAL.AccessData.Msg, MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                m.execute_data("delete from "+m.user+".dmviettat where id="+l_id);
                load_grid(); reft_text();
            }
        }
    }
}